package com.hzdaba.aiapi.configuration;

import com.hzdaba.aiapi.handlerInterceptor.CheckReqIdHandlerInterceptor;
import com.hzdaba.aiapi.handlerInterceptor.CheckTokenHandlerInterceptor;
import com.hzdaba.aiapi.handlerInterceptor.CheckWhiteListHandlerInterceptor;
import com.hzdaba.aiapi.handlerInterceptor.CurrentLimitingHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置拦截器
 *
 * @author 刘逸晖
 */
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {

    /**
     * 校验请求 ID 的拦截器
     */
    @Autowired
    private CheckReqIdHandlerInterceptor checkReqIdHandlerInterceptor;

    /**
     * 校验 token 的拦截器
     */
    @Autowired
    private CheckTokenHandlerInterceptor checkTokenHandlerInterceptor;

    /**
     * 校验白名单的拦截器
     */
    @Autowired
    private CheckWhiteListHandlerInterceptor checkWhiteListHandlerInterceptor;

    /**
     * 流控拦截器
     */
    @Autowired
    private CurrentLimitingHandlerInterceptor currentLimitingHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String pathOfLogin = "/api/customer/login";//处理登录请求的路径
        String pathOfError = "/error/**";// 处理错误请求的路径
        String[] includablePath = {"/**"};//包含的路径

        //添加根据 ip 限流的拦截器
        registry.addInterceptor(this.currentLimitingHandlerInterceptor)
                .excludePathPatterns(pathOfError)
                .addPathPatterns(includablePath)
                .order(0);

        //添加校验请求 id 的拦截器
        registry.addInterceptor(this.checkReqIdHandlerInterceptor)
                .excludePathPatterns(pathOfError)
                .addPathPatterns(includablePath)
                .order(1);

        //添加校验 token 的拦截器
        registry.addInterceptor(this.checkTokenHandlerInterceptor)
                .excludePathPatterns(pathOfError, pathOfLogin)
                .addPathPatterns(includablePath)
                .order(2);

        //添加校验白名单的拦截器
        registry.addInterceptor(this.checkWhiteListHandlerInterceptor)
                .excludePathPatterns(pathOfError, pathOfLogin)
                .addPathPatterns(includablePath)
                .order(3);

    }
}
